#include "CInputManager.h"

std::auto_ptr<CInputManager> CInputManager::m_instance;

CInputManager::CInputManager()
{
}

CInputManager::~CInputManager()
{
    Release();
}

bool CInputManager::Release()
{
    SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    return true;
}

bool CInputManager::Initialize()
{
    int iResult = SDL_InitSubSystem(SDL_INIT_JOYSTICK);
    SDL_EnableKeyRepeat(1,1);
    if (iResult < 0)
        return false;
    return true;
}

bool CInputManager::Read()
{
    while (SDL_PollEvent(&Event))
        continue;
    return true;
}

bool CInputManager::KeyDown(SDLKey keyPam)
{
    if (Event.key.type   == SDL_KEYDOWN &&
            Event.key.keysym.sym == keyPam)
        return true;
    return false;
}

bool CInputManager::KeyPressed(SDLKey keyPam)
{
    if (Event.key.state  == SDL_PRESSED &&
            Event.key.keysym.sym == keyPam)
        return true;
    return false;
}

bool CInputManager::KeyReleased(SDLKey keyPam)
{
    if (Event.key.state  == SDL_RELEASED &&
            Event.key.keysym.sym == keyPam)
        return true;
    return false;
}

bool CInputManager::MouBtnDown(unsigned int buttonPam)
{
    if (Event.button.type   == SDL_MOUSEBUTTONDOWN &&
            Event.button.button == buttonPam)
        SetPositionClick();
        return true;
    return  false;
}

bool CInputManager::MouBtnPressed(unsigned int buttonPam)
{
    if (Event.button.type   == SDL_MOUSEBUTTONDOWN &&
            Event.button.button == buttonPam)
    {
        SetPositionClick();
        return true;
    }

    return false;
}

bool CInputManager::MouBtnReleased(unsigned int buttonPam)
{
    if (Event.button.type   == SDL_MOUSEBUTTONUP &&
            Event.button.button == buttonPam)
    {
        SetPositionClick();
        return true;
    }

    return false;
}


bool CInputManager::EventType(unsigned int TypePam)
{
    if (Event.type == TypePam)
        return true;
    return false;
}

void CInputManager::SetPositionClick()
{
    MousePos.fx = Event.motion.x;
    MousePos.fy = Event.motion.y;
}

void CInputManager::SetWindowsMousePos()
{

}

bool CInputManager::KeyState(SDLKey keyPam)
{
    Uint8 *keystate = SDL_GetKeyState(NULL);
    if(keystate[keyPam])
        return true;
    return false;
}
